package cn.itsource._06LucenTest;

import java.io.IOException;
import java.nio.file.Paths;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.junit.Test;

/**
 * @description: Lucene入门案例
 */
public class LuceneTest {

    /**
    * @description: 创建索引
    * @author: CTL
    * @date: 2023/6/14 18:04
    * @param
    * @return
    */
    @Test
    public void createIndex() throws IOException {

        //模拟数据库的数据
        //第1行数据的两个字段
        Long id1 = 1L;
        String name1 = "hello world";
        //第2行数据的两个字段
        Long id2 = 2L;
        String name2 = "hello lucene";
        //第3行数据的两个字段
        Long id3 = 3L;
        String name3 = "oh my hello lucene elasticsearch";

        //存放索引的目录
        Directory directory = new SimpleFSDirectory(Paths.get("D:\\WorkspaceIDEA\\projeck3\\RabbitMQ_demo_git\\src\\index"));

        //创建索引的相关配置
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new SimpleAnalyzer());

        //索引创建器
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);

        //开始数据封装
        Document document1 = new Document();
        document1.add(new LongField("id", id1, Field.Store.YES));
        document1.add(new TextField("name", name1, Field.Store.YES));

        Document document2 = new Document();
        document2.add(new LongField("id", id2, Field.Store.YES));
        document2.add(new TextField("name", name2, Field.Store.YES));

        Document document3 = new Document();
        document3.add(new LongField("id", id3, Field.Store.YES));
        document3.add(new TextField("name", name3, Field.Store.YES));

        //把文档添加到索引创建器
        indexWriter.addDocument(document1);
        indexWriter.addDocument(document2);
        indexWriter.addDocument(document3);

        //提交
        indexWriter.commit();
        indexWriter.close();
    }

    @Test
    public void seraryIndex() throws IOException {

        //存放索引的目录
        Directory directory = new SimpleFSDirectory(Paths.get("D:\\WorkspaceIDEA\\projeck3\\RabbitMQ_demo_git\\src\\index"));

        //索引读取器
        IndexReader indexReader = DirectoryReader.open(directory);

        //搜索器
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);

        //执行搜索
        //查询条件：查询name中包含"lucene"，用TermQuery进行单词查询
        Query query = new TermQuery(new Term("name", "lucene"));//第一个参数：查询字段名称；第二个字段：查询值
        //查询10条
        TopDocs topDocs = indexSearcher.search(query, 10);

        //总条数
        int totalHits = topDocs.totalHits;
        System.out.println("总条数："+totalHits);

        //带分数的文档结果列表
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc scoreDoc : scoreDocs) {
            System.out.println("=================================");
            System.out.println("分数：" + scoreDoc.score);
            System.out.println("编号：" + scoreDoc.doc);

            //根据文档编号，获取文档对象
            Document doc = indexSearcher.doc(scoreDoc.doc);
            System.out.println("原始数据ID：" + doc.get("id"));
            System.out.println("原始数据name：" + doc.get("name"));
        }
    }
}
